/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import com.cmcc.zysoft.infocollection.model.Menu;
import com.cmcc.zysoft.infocollection.model.Role;
import com.cmcc.zysoft.spring.security.model.User;
import com.starit.common.dao.hibernate.HibernateBaseDaoImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: MenuPcDao.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-6 上午10:10:21
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Repository
public class MenuPcDao extends HibernateBaseDaoImpl<Menu, String> {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 获取菜单树.
	 * @param parentId 父菜单ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> menuTree(String parentId) {
		String sql = 
				" SELECT a.menu_id AS id, a.menu_name AS 'text', a.is_leaf AS isLeaf, a.is_leaf AS attributes, a.path AS path, a.*, " +
				" b.company_name AS companyName, (CASE a.is_leaf WHEN 'N' THEN 'closed' WHEN 'Y' THEN 'open' ELSE 'closed' END) AS state " +
				" FROM tb_c_menu a, tb_c_company b " +
				" WHERE a.company_id = b.company_id AND a.status = '1' AND a.parent_id = ? " +
				" ORDER BY a.display_order ASC ";
		return this.jdbcTemplate.queryForList(sql, parentId);
	}
	
	/**
	 * 根据父菜单ID获取其子菜单.
	 * @param parentId 父菜单ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> getSubMenu(String parentId) {
		String sql = 
				" SELECT menu.menu_id AS menuId, menu.parent_id AS parentId, menu.is_leaf AS isLeaf, menu.menu_name AS menuName " +
				" FROM tb_c_menu menu " +
				" WHERE menu.status = '1' AND menu.parent_id = ?";
		return this.jdbcTemplate.queryForList(sql, parentId);
	}
	
	/**
	 * 根据父菜单ID获取子菜单列表.
	 * @param pid 父菜单ID
	 * @param user 当前用户信息
	 * @return List<Menu>
	 */
	@SuppressWarnings("unchecked")
	public List<Menu> getMenusByPid(final String pid, User user) {
		//获取用户所拥有的角色列表
		final List<Role> roles = user.getRoles();
		final String hql = 
				" select distinct m from Menu as m, RoleMenu as rm, Role as r " +
				" where m.menuId = rm.menuId and r.roleId = rm.roleId and m.status = '1' and r.status = '0' " +
				" and m.companyId = r.companyId " +
				" and m.parentId = :parentId " +
				" and r in (:roles) order by m.displayOrder " ;
		List<Menu> menus = this.getHibernateTemplate().executeFind(
			new HibernateCallback<List<Menu>>() {
				public List<Menu> doInHibernate(Session session) throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
					query.setParameter("parentId", pid);
					query.setParameterList("roles", roles);
					List<Menu> list = query.list();
					return list;
				}
			});
		return menus;
	}
	
	/**
	 * 根据菜单名称搜索菜单.
	 * @param menuName 菜单名称
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> searchMenu(String menuName) {
		String sql = "SELECT * FROM tb_c_menu menu " +
				" WHERE menu.status = '1' AND menu.menu_name like '%" + menuName.trim() + "%' ";
		return this.jdbcTemplate.queryForList(sql);
	}
	
	/**
	 * 通过菜单ID查找菜单信息.
	 * @param menuId 菜单ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> searchMenuByMenuId(String menuId) {
		String sql = "SELECT * FROM tb_c_menu menu WHERE menu.status = '1' AND menu.menu_id = ? ";
		return this.jdbcTemplate.queryForList(sql, menuId);
	}
}